简单实现一个useSignal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
export const useSignal = (initialState) => {
const [state, setState] = useState(initialState)
const stateRef = useRef(initialState)

const getState = () => stateRef.current

const editState = (newState) => {
setState(newState)

if (typeof newState === 'function'){
stateRef.current = newState(stateRef.current)
} else {
stateRef.current = newState
}
}

return [state, editState, getState]
}
作者

vear

发布于

2024-12-19

更新于

2024-12-19

许可协议

评论